﻿using CsvHelper;
using MMCInfo.Lib;
using MMCInfo.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.DataVisualization.Charting;

namespace MMCInfo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public FileResult GetChart()
        {
            //Chart-object
            Chart chart = new Chart();

            chart.Width = 800;
            chart.Height = 600;
            chart.RenderType = RenderType.ImageTag;

            chart.Palette = ChartColorPalette.BrightPastel;
            Title t = new Title("S&P 500 Historie", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
            chart.Titles.Add(t);

            chart.ChartAreas.Add("ChartArea");
            chart.ChartAreas["ChartArea"].AxisX.Interval = 1;
            chart.ChartAreas["ChartArea"].AxisX.IntervalType = DateTimeIntervalType.Months;
            chart.ChartAreas["ChartArea"].AxisX.IntervalOffset = 1;
            chart.ChartAreas["ChartArea"].AxisX.IntervalOffsetType = DateTimeIntervalType.Months;

            string stockSymbol = "";
            int counter = 10;

            foreach (StockModel stock in getCsvData())
            {
                if (counter == 0)
                {
                    break;
                }
                if (!stockSymbol.Equals(stock.symbol))
                {
                    stockSymbol = stock.symbol;
                    chart.Series.Add(stockSymbol);
                    chart.Series[stockSymbol].ChartType = SeriesChartType.Line;

                    // Set the X value type of the displayed series to DateTime.
                    chart.Series[stockSymbol].XValueType = ChartValueType.DateTime;

                    counter--;
                }
                chart.Series[stockSymbol].Points.AddXY(stock.date, stock.close);
            }

            chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            chart.BorderlineWidth = 2;
            chart.BorderColor = System.Drawing.Color.Black;
            chart.BorderlineDashStyle = ChartDashStyle.Solid;
            chart.BorderWidth = 2;
            chart.Legends.Add("Legend1");

            return new ChartResult(chart, ChartImageFormat.Png) { FileDownloadName = "chart.png" };
        }

        public IEnumerable<StockModel> getCsvData()
        {
            string csvPath = Path.Combine(System.Web.HttpContext.Current.Request.PhysicalApplicationPath, "App_Data", "sp500hst.txt");
            CsvReader csv = new CsvReader(new StreamReader(csvPath));

            IEnumerable<StockModel> stockList = csv.GetRecords<StockModel>();

            return stockList;
        }
    }
}
